/**
 * 
 */
package net.mikoo.seals.boss.web.interceptor;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import net.mikoo.seals.boss.biz.services.RolePermitService;
import net.mikoo.seals.boss.biz.services.UserLoginServices;
import net.mikoo.seals.boss.common.WebContext;
import net.mikoo.seals.boss.common.enums.PartyTypeEnum;
import net.mikoo.seals.boss.model.Menu;
import net.mikoo.seals.boss.model.Party;
import net.mikoo.seals.boss.model.Person;


/**
 * @author huangxm
 *
 */
public class AdminInterceptor implements HandlerInterceptor  {

	/**
	 * 
	 */
	
	@Autowired
	private UserLoginServices userLoginServices;
	
	@Autowired
	private RolePermitService rolePermitService;
	
	@Autowired
    private ServletContext servletContext;
	
	public AdminInterceptor() {
	}
	
	public void  autoLogin(HttpServletRequest request,ServletContext servletContext){
		//String username = servletContext.getInitParameter("username");
		//String password = servletContext.getInitParameter("password");
		String username = "admin";
		String password = "111111";
		
		if(StringUtils.isNotEmpty(username.toString()) && StringUtils.isNotEmpty(password.toString())){
			HttpSession session = request.getSession();
			Object obj = session.getAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			if(obj == null){
				Party party = userLoginServices.authLogin(username,password,PartyTypeEnum.OPERATOR.getCode());
				if(party != null){
					session.setAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY, party);
				}
			}
		}
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		//autoLogin(request,servletContext);
		initActionState(request);

		HttpSession session = request.getSession();
		Object obj = session.getAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY);
		if(obj != null){
			
			Party party = (Party)obj;
			Person person = party.getPerson();
			if(StringUtils.isNotBlank(person.getName())) {
				
				request.setAttribute("_loginName", person.getName());
			}else {
				request.setAttribute("_loginName", party.getUserLoginId());
			}
			request.setAttribute("_portrait", person.getPortrait());
			
			Object _menus = session.getAttribute("_menus");
			if(_menus == null) {
				List<Menu> menus = rolePermitService.loadHaveResourceAsTree(party.getPartyId());
				session.setAttribute("_menus", menus);
			}
			
			
			
			return true;
		}
		response.sendRedirect("/admin/login.htm");
		return false;
	}
	
	
	public void initActionState(HttpServletRequest request) {
		try {
			String path = request.getServletPath();
			Pattern p = Pattern.compile("\\/(.*?)\\/");// 正则表达式，取q#和#q之间的字符串，不包括q#和#q
			Matcher m = p.matcher(path);
			m.find(1);
			request.setAttribute("_actions", m.group(1));
			System.out.println(m.group(1));// m.group(1)不包括这两个字符
			
			System.out.println("========================");
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}



	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}


}
